package com.noah.project.controller

import com.noah.project.global.Const
import com.noah.project.global.JsonManager
import com.noah.project.dao.BookDao
import com.noah.project.global.TokenManager
import com.noah.project.model.Book
import com.noah.project.model.Reply
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
class BookApiController {
    @Autowired
    lateinit var bookDao: BookDao
    val log = LoggerFactory.getLogger(BookApiController::class.simpleName)
    /*****************日记本******************/
    @RequestMapping(Const.API_ADD_BOOK)
    fun addBook(book: Book, token: String?): Reply {//添加
        try {
            if (!TokenManager.checkToken(token)) {
                return Reply(code = Const.FAIL_TOKEN, msg = "Token失效")
            }
            book.bTime = System.currentTimeMillis()
            val retBid = bookDao.insert(book)
            if (retBid > 0) {
                return Reply(code = Const.SUCCESS, msg = "创建成功", data = JsonManager.toJson(book))
            }
            return Reply(code = Const.FAIL, msg = "创建失败")
        } catch (e: Exception) {
            log.error("创建日记本", e)
            return Reply(code = Const.FAIL_REQUEST, msg = "日记本异常")
        }
    }

    @RequestMapping(Const.API_DELETE_BOOK)
    fun deleteBook(bid: Long, token: String?): Reply {
        try {
            if (!TokenManager.checkToken(token)) {
                return Reply(code = Const.FAIL_TOKEN, msg = "Token失效")
            }
            val ret = bookDao.delete(bid)
            if (ret > 0) {
                return Reply(code = Const.SUCCESS, msg = "删除成功")
            }
            return Reply(code = Const.FAIL, msg = "删除失败")
        } catch (e: Exception) {
            log.error("删除日记本", e)
            return Reply(code = Const.FAIL_REQUEST, msg = "日记本异常")
        }
    }

    @RequestMapping(Const.API_UPDATE_BOOK)
    fun updateBook(book: Book, token: String?): Reply {
        try {
            if (!TokenManager.checkToken(token)) {
                return Reply(code = Const.FAIL_TOKEN, msg = "Token失效")
            }
            val ret = bookDao.update(book)
            if (ret > 0) {
                val retBook = bookDao.findById(book.bid)
                return Reply(code = Const.SUCCESS, msg = "修改成功", data = JsonManager.toJson(retBook))
            }
            return Reply(code = Const.FAIL, msg = "修改失败")
        } catch (e: Exception) {
            log.error("更新日记本", e)
            return Reply(code = Const.FAIL_REQUEST, msg = "日记本异常")
        }
    }

    @RequestMapping(Const.API_GET_BOOK_LIST)
    fun getBookList(uid: Long, token: String?): Reply {
        try {
            if (!TokenManager.checkToken(token)) {
                return Reply(code = Const.FAIL_TOKEN, msg = "Token失效")
            }
            val retBooks = bookDao.findBooksOfUser(uid)
            if (retBooks.isNotEmpty()) {
                return Reply(code = Const.SUCCESS, msg = "获取日记本列表成功", data = JsonManager.toJson(retBooks))
            }
            return Reply(code = Const.FAIL, msg = "没有更多日记本")
        } catch (e: Exception) {
            log.error("获取日记本列表", e)
            return Reply(code = Const.FAIL_REQUEST, msg = "日记本异常")
        }
    }

    @RequestMapping(Const.API_GET_BOOK)
    fun getBook(bid: Long, token: String?): Reply {
        try {
            if (!TokenManager.checkToken(token)) {
                return Reply(code = Const.FAIL_TOKEN, msg = "Token失效")
            }
            val retBook = bookDao.findById(bid)
            if (retBook != null) {
                return Reply(code = Const.SUCCESS, msg = "获取日记本成功", data = JsonManager.toJson(retBook))
            }
            return Reply(code = Const.FAIL, msg = "找不到该日记本")
        } catch (e: Exception) {
            log.error("获取单个日记本", e)
            return Reply(code = Const.FAIL_REQUEST, msg = "日记本异常")
        }
    }
}